package 基本线程机制.新类库中的构件;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class GreenhouseScheduler {//调度器  skɛdʒʊld 与timer类似定时执行
	private volatile boolean light=false;//光
	private volatile boolean water=false;//水
	private String thermostat="Day";
	public synchronized String getThermostat(){
		return thermostat;
	}
	public synchronized void setThermostat(String value){
		thermostat=value;
	}
	ScheduledThreadPoolExecutor scheduler=new ScheduledThreadPoolExecutor(10); 
	public void schedule(Runnable event,long delay){
		scheduler.schedule(event,delay, TimeUnit.MILLISECONDS);//创建一个一次性的延时调度，把线程添加到调度器中
	}
	public void repeat(Runnable event,long initialDelay,long period){//重复
		scheduler.scheduleAtFixedRate(event, initialDelay, period, TimeUnit.MICROSECONDS);//创建一个周期性的调度，第一次调度是initialDelay  随后是 initialDelay+2*period
	}
	class LightOn implements Runnable{//打开光
		public void run(){
			System.out.println("Turning on lights");
			light=true;
		}
	}
	class LightOff implements Runnable{//关掉光
		public void run (){
			System.out.println("Turning off lights");
			light=false;
		}
	}
	class WaterOn implements Runnable{//给水
		public void run(){
			System.out.println("Turning greenhouse water on");
			water=true;
		}
	}
	class WaterOff implements Runnable{//给水
		public void run(){
			System.out.println("Turning greenhouse water off");
			water=false;
		}
	}
	class ThermostatNight implements Runnable{
		public void run(){
			System.out.println("Thermostat to night setting");
			setThermostat("Night");
		}
	}
	class ThermostatDay implements Runnable{
		public void run(){
			System.out.println("Thermostat to day setting");
			setThermostat("Day");
		}
	}
	class Bell implements Runnable{
		public void run(){
			System.out.println("Bing!");
		}
	}
	class Terminate implements Runnable{
		public void run (){
			System.out.println("Terminating");
			scheduler.shutdown();
			new Thread(){
				public void run(){
//					for(DataPoint){}
				}
			}.start();
		}
	}
	static class DataPoint{
		final Calendar time;
		final float temperature;
		final float humidity;
		public DataPoint(Calendar d,float temp,float hum){
			time=d;
			temperature=temp;
			humidity=hum;
		}
		public String toString(){
			return time.getTime()+String.format(" Temperature:%1$.1f humidity:%2$.2f", temperature,humidity);
		}
	}
	private Calendar lastTime =Calendar.getInstance();
	{
		lastTime.set(Calendar.MINUTE, 30);
		lastTime.set(Calendar.SECOND, 00);
	}
	private float lastTemp=65.0f;
	private int tempDirection=+1;
	private float lastHumidity=50.0f;
	private int humidityDirection=+1;
	private Random rand=new Random(47);
	List<DataPoint> data=Collections.synchronizedList(new ArrayList<>());
	class CollectData implements Runnable{
		public void run(){
			System.out.println("Collecting data");
			synchronized(GreenhouseScheduler.this){
				lastTime.set(Calendar.MINUTE,lastTime.get(Calendar.MINUTE)+30);
				if(rand.nextInt(5)==4){
					tempDirection=-tempDirection;
					lastTemp=lastTemp+humidityDirection*rand.nextFloat();
					data.add(new DataPoint((Calendar)lastTime.clone(), lastTemp,lastHumidity));
				}
			}
		}
	}
	public static void main(String[] args) {
		GreenhouseScheduler gh=new GreenhouseScheduler();
		gh.schedule(gh.new Terminate(), 5000);
		gh.repeat(gh.new Bell(), 0, 1000);
		gh.repeat(gh.new ThermostatNight(), 0, 2000);
		gh.repeat(gh.new LightOn(), 0, 200);
		gh.repeat(gh.new LightOff(), 0, 400);
		gh.repeat(gh.new WaterOn(), 0, 600);
		gh.repeat(gh.new WaterOff(), 0, 800);
		gh.repeat(gh.new ThermostatDay(), 0, 1400);
		gh.repeat(gh.new CollectData(), 500, 500);
	}
}
